// pages/item/item.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",
    pageNo: 1,     //当前页码
    limit: 7,     //每页显示记录数
    total: 0,      //总记录数
    totalPage:0,     //总页数
    items:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log("题目列表页："+options.id);
    this.setData({
      id: options.id
    })
    this.loadContextList();
    this.loadQuestionList();
  },
  //加载广告列表数据
  loadContextList:function() {
    var that = this;
    var baseUrl = app.globalData.baseUrl;
    wx.request({
      url: baseUrl+'/context/banner/list',
      method:'GET',
      header:{'Authorization':wx.getStorageSync('token')},
      success:function(res) {
        //console.log(res);
        that.setData({
          imgurls:res.data.page.list
        })
        
      }
    })
  },


  loadQuestionList:function(){
    var that = this;
    var baseUrl = app.globalData.baseUrl;
    wx.request({
      url: baseUrl+'/question/question/list?page='+that.data.pageNo+'&limit='+that.data.limit+'&key='+that.data.id,
      method:'GET',
      header:{'Authorization':wx.getStorageSync('token')},
      success:function(res){
        //console.log(res);
        that.setData({
          items:res.data.page,
          total:res.data.page.totalConunt,
          totalPage:res.data.page.totalPage
        })
      }
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var pageNo = this.data.pageNo;
    
      this.setData({
        pageNo: pageNo+1
      })
      if(this.data.pageNo<=this.data.totalPage){
        var that = this;
        var baseUrl = app.globalData.baseUrl;
        wx.request({
          url: baseUrl+'/question/question/list?page='+that.data.pageNo+'&limit='+that.data.limit+'&key='+that.data.id,
          method:'GET',
          header:{'Authorization':wx.getStorageSync('token')},
          success:function(res){
            //需要将下一页的集合和之前的集合合并
            that.data.items.list.push.apply(that.data.items.list,res.data.page.list);

            //console.log(res);
            that.setData({
              //items:res.data.page,
              items: that.data.items,
              total:res.data.page.totalConunt,
              totalPage:res.data.page.totalPage
            })
          }
        })
      }else{
        wx.showToast({
          title: '已到达最后一页',
          icon:'error',
          duration:2000
        })
      }


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getInfo:function(e){
    //console.log(e.currentTarget.dataset.id);
    //跳转到题目详情页并传递参数
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../info/info?id='+id,
    })
     
  }
})